'use strict';

app.controller('brandCtrl',['$scope','$modal','utilService', function($scope,$modal,utilService){
     $scope.brand = {};

    $scope.query = function(){
        utilService.queryForPagingList($scope,{
            url:$scope.app.domain + 'chain/brand',
            params:$scope.brand
        }).then(function(result){
        });
    }
    $scope.query();

    //删除品牌
    $scope.openDelete = function (id,name) {
        var modalInstance = $modal.open({
            templateUrl: 'tpl/brand/brand_delete.html',
            controller: 'delBrandlCtrl',
            scope: $scope,
            size:'sm',
            resolve: {
                data: function () {
                    var data = {};
                    data.id = id;
                    data.name=name;
                    return data;
                }
            }
        });
    }

}]);

app.controller('createBrandCtrl',['$scope','$state','brandService','toaster', function($scope,$state,brandService,toaster){

    $scope.isShowImage = false;

    $scope.uploadFile = function (event) {
        console.info(event);
    };
    $scope.AllowToAdd=true;
    $scope.createBrand = function(){
        if(!$scope.AllowToAdd){
            return;
        }
        $scope.AllowToAdd=false;
        brandService.createBrand($scope).then(function(result){
            switch (result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $scope.AllowToAdd=true;
                    $state.go('app.brand');
                    break;
                case 614:
                    toaster.pop('waiting', "提示", result.message);
                    $scope.AllowToAdd=true;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    $scope.AllowToAdd=true;
                    break
            }
            //$state.go('app.account');
        });
    }
}]);

//新增品牌controller
app.controller('addBrandCtrl',['$scope','$state','brandService','toaster', function($scope,$state,brandService,toaster){
    $scope.AllowToAdd=true;
    $scope.addBrand = function(){
        if(!$scope.AllowToAdd){
            return;
        }
        $scope.AllowToAdd=false;
        $scope.brand.logoImgUrl=brandService.fileName;
        brandService.addBrand($scope).then(function(result){
            switch (result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $scope.AllowToAdd=true;
                    $state.go('app.brand');
                    break;
                case 614:
                    toaster.pop('waiting', "提示", result.message);
                    $scope.AllowToAdd=true;
                    break;
                default:
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    $scope.AllowToAdd=true;
                    break
            }
        });
    }
}]);
//修改品牌Controller
app.controller('editBrandCtrl',['$scope','$state','$stateParams','brandService','toaster', function($scope,$state,$stateParams,brandService,toaster){
    $scope.brand = {
        id:$stateParams.id
    };
    //根据id查询品牌
    brandService.findById($scope).then(function(result){
        switch (result.status){
            case 200:
                $scope.brand = result.data;
                if($scope.brand.logoImgUrl!=null){
                    $scope.$broadcast('hasPicture',true);
                }
                break;
            case 615:
                toaster.pop('waiting', "提示", result.message);
                break;
            default :
                toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                break
        }
    });
    //修改品牌
    $scope.AllowToEdit=true;
    $scope.editBrand = function(){
        if(!$scope.AllowToEdit){
            return;
        }
        $scope.AllowToEdit=false;
        if("undefined"  != typeof brandService.fileName){
            $scope.brand.logoImgUrl=brandService.fileName;
        }
        brandService.editBrand($scope).then(function(result){
            switch (result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $scope.AllowToEdit=true;
                    $state.go('app.brand');
                    break;
                case 604:
                case 614:
                case 615:
                    toaster.pop('waiting', "提示", result.message);
                    $scope.AllowToEdit=true;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    $scope.AllowToEdit=true;
                    break
            }
        });
    }

    //删除图片路径
    $scope.delBrandPic=function(){
        brandService.delBrandPic($scope).then(function(result){
            switch (result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $scope.$broadcast('hasPicture',false);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
    }

}]);

//品牌详情Controller
app.controller('brandDetailCtrl',['$scope','$state','$stateParams','brandService','toaster', function($scope,$state,$stateParams,brandService,toaster){
    $scope.brand = {
        id:$stateParams.id
    };
    //根据id查询品牌
    brandService.findById($scope).then(function(result){
        switch (result.status){
            case 200:
                $scope.brand = result.data;
                break;
            case 615:
                toaster.pop('waiting', "提示", result.message);
                break;
            default :
                toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                break
        }
    });
}]);

//删除模态框Controller
app.controller('delBrandlCtrl', ['$scope', '$modalInstance','brandService','toaster','data',
    function($scope, $modalInstance,brandService,toaster,data) {
        $scope.brand=data;

        $scope.AllowToDelete=true;

        //确认删除
        $scope.ok=function(){
            if(!$scope.AllowToDelete){
                return;
            }
            $scope.AllowToDelete=false;
            brandService.delBrand($scope).then(function(result){
                switch (result.status){
                    case 200:
                        $modalInstance.dismiss('success');
                        toaster.pop('success', "提示", "操作成功");
                        $scope.AllowToDelete=true;
                        $scope.query();
                        break;
                    case 615:
                        toaster.pop('waiting', "提示", result.message);
                        $scope.AllowToDelete=true;
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        $scope.AllowToDelete=true;
                        break;
                }
            });
        }
        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }
]);


/*上传图片controller*/
app.controller('fileUploadCtrl', ['$scope', 'FileUploader','$cookies','toaster','fileUploadService', 'brandService',function($scope, FileUploader,$cookies,toaster,fileUploadService,brandService) {
    $scope.uploadStatus = $scope.uploadStatus1 = false;
    $scope.queueLimit=1;
    $scope.hasPicture=false;

    $scope.$on("hasPicture",function (event, msg) {
        $scope.hasPicture = msg;
    });

    var uploader = $scope.uploader = new FileUploader({
        url: $scope.app.domain+'/upload',
        formData:[{sessionId:$cookies.sessionId}],
        queueLimit:$scope.queueLimit
    });

    var status=false;
    // FILTERS
    uploader.filters.push({
        name: 'picFilter',
        fn: function(item, options) {
            if(item.size>2*1024*1024){
                toaster.pop('waiting', "添加图片失败", "文件太大,超出2M大小");
                return false;
            }
            if(!(item.type=='image/png' ||item.type=='image/gif' || item.type=='image/jpeg' || item.type=='image/jpg')){
                toaster.pop('waiting', "添加图片失败", "不属于图片类型");
                return false;
            }

            if(this.queue.length > $scope.queueLimit){
                toaster.pop('waiting', "添加图片失败", "超出图片上传数量");
                return false;
            }

            return true;
        }
    });

    // CALLBACKS
    uploader.onWhenAddingFileFailed = function(item,filter, options) {
        console.info('添加文件失败', item, filter, options);
    };
    uploader.onAfterAddingFile = function(fileItem) {
        if(this.queue.length > $scope.queueLimit){
            toaster.pop('waiting', "添加图片失败", "超出图片上传数量");
            return false;
        }
    };

    uploader.onAfterAddingAll = function(addedFileItems) {
        if(!uploader.getNotUploadedItems().length){
            return;
        }
        uploader.uploadAll();
    };
    uploader.onBeforeUploadItem = function(item) {

    };
    uploader.onProgressItem = function(fileItem, progress) {
        console.info('上传一个文件中...', fileItem, progress);
    };
    uploader.onProgressAll = function(progress) {
        console.info('上传所有文件中...', progress);
    };
    uploader.onSuccessItem = function(fileItem, response, status, headers) {
        console.info('上传成功的文件', fileItem, response, status, headers);
        switch (status){
            case 200:
                brandService.fileName=response.data;
                fileItem.fileName=response.data;
                fileItem.imgsrc=$scope.app.image.temp+response.data;
                break;
            default :
                break;
        }

    };

    uploader.onErrorItem = function(fileItem, response, status, headers) {
        console.info('上传失败的文件', fileItem, response, status, headers);
        toaster.pop('waiting', "上传图片失败", "未知错误");
    };

    uploader.onCancelItem = function(fileItem, response, status, headers) {
        console.info('取消上传的文件', fileItem, response, status, headers);
    };

    uploader.onCompleteItem = function(fileItem, response, status, headers) {
        console.info('完成上传的文件', fileItem, response, status, headers);
    };

    uploader.onCompleteAll = function() {
        console.info('所有文件上传完成');
    };

    $scope.del=function(item){
        fileUploadService.del($scope,item).then(function(result){
            switch (result.status){
                case 200:
                    item.remove();
                    break;
                default :
                    break;
            }
        });
    }

    //监听父容器广播的数据
    $scope.$on('goodsImages',function(e,msg){
        //获取到父控制器广播的内容；
        $scope.hasPicture=msg;
    });

}]);
